﻿using System.Linq;
using System.Web.Mvc;
using CultuRAM.Models;
using PagedList;
using CultuRAM.DAL;

namespace CultuRAM.Controllers
{
    [Authorize]
    public class EntityController : Controller
    {
        //private CultuRAMContext db = new CultuRAMContext();
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        //
        // GET: /Entity/

        public ActionResult Index(int? page)
        {
            var entities = _unitOfWork.EntityRepository.Get().OrderBy(x => x.Name);

            if (Request.HttpMethod == "GET")
            {
            }
            else
            {
                page = 1;
            }

            int pageSize = 8;
            int pageNumber = (page ?? 1);

            return View(entities.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /Entity/Details/5

        public ActionResult Details(int id = 0)
        {
            Entity entity = _unitOfWork.EntityRepository.GetByID(id);
            if (entity == null)
            {
                return HttpNotFound();
            }
            return View(entity);
        }

        //
        // GET: /Entity/Create

        public ActionResult Create()
        {
            PopulateCityDropDownList();
            PopulateEntityTypeDropDownList();
            return View();
        }

        //
        // POST: /Entity/Create

        [HttpPost]
        public ActionResult Create(Entity entity)      
        {

            if (ModelState.IsValid)
            {
                _unitOfWork.EntityRepository.Insert(entity);
                //var listEntidades = _unitOfWork.EntityRepository.Get();
                //var ent = _unitOfWork.EntityRepository.Get().LastOrDefault();
                _unitOfWork.Save();
                return RedirectToAction("Index");
            }

            PopulateCityDropDownList(entity.CityId);
            PopulateEntityTypeDropDownList(entity.EntityTypeId);
            return View(entity);
        }

        //
        // GET: /Entity/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Entity entity = _unitOfWork.EntityRepository.GetByID(id);
            if (entity == null)
            {
                return HttpNotFound();
            }
            
            PopulateCityDropDownList(entity.CityId);
            PopulateEntityTypeDropDownList(entity.EntityTypeId);
            return View(entity);
        }

        //
        // POST: /Entity/Edit/5

        [HttpPost]
        public ActionResult Edit(Entity entity)
        {
            if (ModelState.IsValid)
            {
                _unitOfWork.EntityRepository.Update(entity);              
                _unitOfWork.Save();
                return RedirectToAction("Index");
            }
            
            PopulateCityDropDownList(entity.CityId);
            PopulateEntityTypeDropDownList(entity.EntityTypeId);
            return View(entity);
        }

        //
        // GET: /Entity/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Entity entity = _unitOfWork.EntityRepository.GetByID(id);
            if (entity == null)
            {
                return HttpNotFound();
            }
            return View(entity);
        }

        //
        // POST: /Entity/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Entity entity = _unitOfWork.EntityRepository.GetByID(id);
            _unitOfWork.EntityRepository.Delete(entity);
            _unitOfWork.Save();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            _unitOfWork.Dispose();
            base.Dispose(disposing);
        }


        //AUXILIARY METHODS /////////////////////////////////////////////////////////

        //Method: Populate the DropDownList of Entity Type
        private void PopulateEntityTypeDropDownList(int? selectedEntityTypeId = 0)
        {
            if (selectedEntityTypeId == null)
            {
                var entityTypes = _unitOfWork.EntityTypeRepository.Get().ToList();
                ViewBag.EntityTypeId = new SelectList(entityTypes, "EntityTypeId", "EntityTypeName");
            }
            else
            {
                var entityTypes = _unitOfWork.EntityTypeRepository.Get().ToList();
                ViewBag.EntityTypeId = new SelectList(entityTypes, "EntityTypeId", "EntityTypeName", selectedEntityTypeId);
            }
        }

        //Method: Populate the DropDownList of City
        private void PopulateCityDropDownList(int? selectedCityId = 0)
        {
            if (selectedCityId == null)
            {
                var cities = _unitOfWork.CityRepository.Get().ToList();
                ViewBag.CityId = new SelectList(cities, "CityId", "CityName");
            }
            else
            {
                var cities = _unitOfWork.CityRepository.Get().ToList();
                ViewBag.CityId = new SelectList(cities, "CityId", "CityName", selectedCityId);
            }
        }
    }
}